Python syntax and semantics
part 21/45 · 74.8 KB total
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Operators
Arithmetic
Python includes the +, -, *, / ("true division"), // (floor division), % (modulus), and ** (exponentiation) operators, with their usual mathematical precedence.
In Python 3, x / y performs "true division", meaning that it always returns a float, even if both x and y are integers that divide evenly.
>>> 4 / 2
2.0
and // performs integer division or floor division, returning the floor of the quotient as an integer.
In Python 2 (and most other programming languages), unless explicitly requested, x / y performed integer division, returning a float only if either input was a float. However, because Python is a dynamically-typed language, it was not always possible to tell which operation was being performed, which often led to subtle bugs, thus prompting the introduction of the // operator and the change in semantics of the / operator in Python 3.
Comparison operators